package com.biz.primus.ms.order.promotion;

import com.biz.primus.base.exception.utils.AssertUtils;
import com.biz.primus.model.order.exception.OrderException;
import com.biz.primus.model.order.vo.request.OrderRequestVO;
import com.biz.primus.model.promotion.vo.OrderPromotionDetailVo;
import com.biz.primus.ms.order.convertor.OrderPromotionConvertor;
import com.biz.primus.ms.order.promotion.helper.OrderPromotionHelper;

/**
 * 抽象促销处理器
 * <p>
 * SECKILL("秒杀", "秒"),
 * CUT_PRICE_SALE("降价拍", "拍"),～～～～～～～
 * DISCOUNT("折扣促销", "折"),～～～～～～
 * PRESELL("预售", "预"),
 * PURCHASE_GIFT("买赠", "赠"),～～～～～～～
 * PURCHASE_LOTTERY("购买抽奖", "奖"),
 * RAISE_PRICE_REDEMPTION("加价换购", "换"),
 * SIGNATURE("签到/评价有礼", "礼"), ～～～～～～～x
 * SPECIAL_OFFER("简单特价", "特"),
 * USER_FIRST_ORDER_CUT("首单立减", "减"),～～～～～～～x
 * LUCKY_ORDER("下单有礼", "礼");
 *
 * @author Tiger Feng
 * @date 2017年11月09日
 */
public abstract class AbstractPromotionHandler {
    /**
     * 订单促销详情
     */
    public OrderPromotionDetailVo orderPromotionDetailVo;
    /**
     * 订单
     */
    public OrderRequestVO orderRequestVO;
    /**
     * 订单促销助理
     */
    public OrderPromotionHelper helper;

    protected AbstractPromotionHandler() {
    }

    protected AbstractPromotionHandler(OrderPromotionDetailVo orderPromotionDetailVo,
                                       OrderRequestVO orderRequestVO,
                                       OrderPromotionHelper helper) {
        this.orderPromotionDetailVo = orderPromotionDetailVo;
        this.orderRequestVO = orderRequestVO;
        this.helper = helper;
    }

    /**
     * 促销执行入口，建议不要重写此方法
     */
    public void execute() {
        // 校验数据
        check();
        // 促销计算
        count();
        // 刷新订单参数
        orderRequestVO.refreshParameter();
    }

    /**
     * 校验数据，建议不要重写此方法
     */
    public void check() {
        // 校验订单
        AssertUtils.notNull(orderRequestVO, OrderException.ORDER_NOT_NULL);
        AssertUtils.notEmpty(orderRequestVO.getItems(), OrderException.ORDER_ITEM_NOT_NULL);
        // 校验促销
        AssertUtils.notNull(orderPromotionDetailVo, OrderException.PROMOTION_HANDLE_EXCEPTION);

        // 校验促销方法是否正确
        AssertUtils.isTrue(checkPromotionType(), OrderException.PROMOTION_HANDLE_EXCEPTION);
        orderRequestVO.setPromotion(OrderPromotionConvertor.detailVO2RequestVO(orderPromotionDetailVo,orderRequestVO));
    }


    /**
     * 校验促销方法是否正确
     *
     * @return
     */
    public abstract boolean checkPromotionType();

    /**
     * 计算金额
     */
    public abstract void count();
}
